import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Longin.vue'
import Home from '../components/Home.vue'


//导入所有子组件  
import User from '../views/user/user.vue'
import Roles from '../views/rights/roles.vue'
import Right from '../views/rights/rights.vue'
import  Goods from '../views/goods/goods.vue'
import Params from '../views/goods/params.vue'
import Categories from '../views/goods/categories.vue'
import Orders from '../views/order/order.vue'
import Reports from '../views/reports/reports.vue'

Vue.use(VueRouter)

const routes = [
{path:"/",redirect:"/login"},   //访问根路径的时候直接到登录页面
{path:"/login",component:Login},
{path:"/home",component:Home,children:[
  {path:"/users",component:User},
  {path:"/roles",component:Roles},
  {path:"/rights",component:Right},
  {path:"/goods",component:Goods},
  {path:"/params",component:Params},
  {path:"/categories",component:Categories},
  {path:"/orders",component:Orders},
  {path:"/reports",component:Reports},
]},
]


//路由拦截      防止有人直接在地址栏直接输入地址没有登录 就进入主页面

//   //  解决路由版本的兼容性的
// const originalPush = VueRouter.prototype.push;
// VueRouter.prototype.push = function push(location) {
//   return originalPush.call(this, location).catch(err => err);
// };



const router = new VueRouter({
  routes
})
router.beforeEach(function (to, from, next) {
  /* 
  1-判断 token 
   如果有token  你已经登陆过   那就放行
   如果没有token  说明没有登录====去登录页面
  
   2-login  如果输入login   就直接放行
  */
  if(to.path=='/login'){
    return next()
  }

  var  token=window.sessionStorage.getItem('token')
  //  输入的是/home  没有登录
  if(!token){
    return next("/login")
  }

  next()
})

export default router
